import baseUrl from '@/baseUrl'
import axios from 'axios'
import { mapMutations } from 'vuex'
export default {
  name: 'login',
  created () {
    if (localStorage.getItem('userinfo')) {
      const userinfo = JSON.parse(decodeURI(localStorage.getItem('userinfo')))
      this.username = userinfo.username
      this.password = userinfo.password
    }
  },
  methods: {
    ...mapMutations(['setUserinfo']),
    remReverse () {
      this.rem = !this.rem
    },
    login () {
      axios.get(`${baseUrl}/users?username=${this.username}&password=${this.password}&roleState=true`)
        .then(res => {
          if (res.data.length === 1) {
            localStorage.setItem('isLogin', true)
            this.setUserinfo(res.data[0])
            if (this.rem) {
              localStorage.setItem('userinfo', encodeURI(JSON.stringify(res.data[0])))
            }
            this.$router.push('/')
          } else {
            alert('用户名或密码错误，请重试！')
          }
        })
    }
  },
  components: {
  },
  data () {
    return {
      rem: true,
      password: '',
      username: ''
    }
  }
}
